package com.koron.css2.newProjectCenter.impl.PubUpWorkRest;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.query.PubUpWorkRestQueryBean;
import com.koron.css2.newProjectCenter.mapper.PubUpWorkRestMapper;
import com.koron.css2.newProjectCenter.vo.PubUpWorkRestListVO;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.*;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

/**
 * 公告
 *
 * @author cd
 */
public class PubUpWorkRestList implements ServerInterface {

    @Override
    @SuppressWarnings("rawtypes")
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            //将入参转换为QueryBean
            PubUpWorkRestQueryBean queryBean = JsonUtils.objectToPojo(req.getData(), PubUpWorkRestQueryBean.class);

            //获取Mapper实例
            PubUpWorkRestMapper mapper = factory.getMapper(PubUpWorkRestMapper.class);
            if (queryBean.getToDo() != null && queryBean.getToDo() == 2) {
                return save (mapper, userInfo, queryBean);
            }
            if (queryBean.getToDo() != null && queryBean.getToDo() == 3) {
                return clearMessage(mapper, queryBean);
            }
            if (queryBean.getToDo() != null && queryBean.getToDo() == 4) {
                return update(mapper,userInfo, queryBean);
            }
            //设置分页参数
            PageHelper.startPage(queryBean.getPage(), queryBean.getPageCount());
            //执行SQL
            List<PubUpWorkRestListVO> list = mapper.selecList(queryBean);

            //输出分页结果
            MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功", PageInfo.class);
            info.setData(new PageInfo<>(list));
            return info;
        } catch (Exception e) {
            factory.close(false);
            logger.error("查询失败" + e.getMessage(), e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
        }
    }


    private MessageBean<?> clearMessage(PubUpWorkRestMapper thisMapper, PubUpWorkRestQueryBean queryBean) {
        if (StringUtils.isEmpty(queryBean.getId())) {
            return MessageBean.create(Constant.NOT_NULL, "主键不能为空", null);
        }
        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        Integer num =  thisMapper.delete(queryBean.getId());
        info.setData(num);
        return info;
    }

    private MessageBean<?> save(PubUpWorkRestMapper thisMapper, UserInfoBean userInfo, PubUpWorkRestQueryBean queryBean) {
        int yeas = 2;
        WorkDaysAndRestDays calendar = new WorkDaysAndRestDays(yeas);
        Map<String, Integer> maps = calendar.printDays();

        for (int i = 0; i <= yeas; i++) {
            int dddd = LocalDate.now().getYear();
            HashMap<String, Boolean> map = new VacationDayCalculate().yearVacationDay(dddd + i);
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                Boolean v = map.get(key);
                if (v) {
                    maps.put(key, 3);
                }

            }
        }
        Set<String> keySet = maps.keySet();
        List<String> relist = new ArrayList<>(keySet);
        if(!relist.isEmpty()){
            thisMapper.deleteListBydata(relist);
        }
        List<PubUpWorkRestQueryBean> lisr=new ArrayList<>();
        for (String key : keySet) {
            PubUpWorkRestQueryBean  nwo=new PubUpWorkRestQueryBean();
              Integer v = maps.get(key);
             Date date=DateUtils.getDateByString(key, "yyyy-MM-dd");
             queryBean.setDataTime(date);
             queryBean.setTage(v);
             queryBean.setCreateAccount(userInfo.getUserInfo().getAcount());
             queryBean.setCreateTime(new DateTime());
             queryBean.setCreateName(userInfo.getUserInfo().getName());
             queryBean.setId(Tools.getObjectId());
            queryBean.setYeas(date.getYear()+1900);

            Calendar calendars = Calendar.getInstance();
            calendars.setTime(date);
            int month = calendars.get(Calendar.MONTH) + 1;
            queryBean.setMonths(month);
            SimpleDateFormat dateFormat = new SimpleDateFormat("EEEE");
            String weekDay = dateFormat.format(date);
            queryBean.setWeek(weekDay);
            BeanUtil.copyProperties(queryBean,nwo);
            lisr.add(nwo);
             //thisMapper.updateOrSave(queryBean);
        }
        thisMapper.brthSave(lisr);
        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        info.setData(keySet.size());
        return info;
    }
    private MessageBean<?> update(PubUpWorkRestMapper thisMapper, UserInfoBean userInfo, PubUpWorkRestQueryBean queryBean) {
         queryBean.setUpdateAccount(userInfo.getUserInfo().getAcount());
         queryBean.setUpdateTime(new DateTime());
         queryBean.setUpdateName(userInfo.getUserInfo().getName());
         int num=thisMapper.updateOrSave(queryBean);
        MessageBean<Integer> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "修改成功", Integer.class);
        info.setData(num);
        return info;
    }
}
